package org.jim.server.demo.config;

import org.jim.core.packets.Command;
import org.jim.server.command.CommandManager;
import org.jim.server.command.handler.*;
import org.jim.server.command.handler.group.CreateGroupHandler;
import org.jim.server.demo.command.DemoWsHandshakeProcessor;
import org.jim.server.demo.service.processor.*;
import org.jim.server.processor.chat.DefaultAsyncChatMessageProcessor;
import org.jim.server.processor.group.GroupCmdProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author lml
 *
 * 以下处理器根据业务需要自行添加与扩展，每个Command都可以添加扩展
 */
/***************** 使用Spring管理处理器 *****************/
@Configuration
public class CommandProcessorConfig {

    private static final Logger log = LoggerFactory.getLogger(CommandProcessorConfig.class);

    /**
     * 握手处理器
     */
    @Bean
    @Autowired
    public HandshakeReqHandler handshakeReqHandler(DemoWsHandshakeProcessor handshakeProcessor) {
        HandshakeReqHandler handler = CommandManager.getCommand(Command.COMMAND_HANDSHAKE_REQ, HandshakeReqHandler.class);
        handler.addMultiProtocolProcessor(handshakeProcessor);
        log.info("============================加载【握手业务处理服务】===========================");
        return handler;
    }

    /**
     * 登录业务处理器
     */
    @Bean
    @Autowired
    public LoginReqHandler loginReqHandler(LoginServiceProcessor loginProcessor) {
        LoginReqHandler handler = CommandManager.getCommand(Command.COMMAND_LOGIN_REQ, LoginReqHandler.class);
        handler.setSingleProcessor(loginProcessor);
        log.info("============================加载【登录业务处理服务】===========================");
        return handler;
    }
    /**
     * 聊天业务处理器
     *
     * 添加用户业务聊天记录处理器，用户自己继承抽象类BaseAsyncChatMessageProcessor即可，以下为内置默认的处理器！
     */
    @Bean
    public ChatReqHandler chatReqHandler() {
        ChatReqHandler handler = CommandManager.getCommand(Command.COMMAND_CHAT_REQ, ChatReqHandler.class);
        handler.setSingleProcessor(chatMessageServiceProcessor()); // 也可以单独声明Bean
        log.info("============================加载【聊天业务处理服务】===========================");
        return handler;
    }

    /**
     * 退出登录 业务处理器
     * @return
     */
    @Bean
    public CloseReqHandler logoutHandler(LogoutServiceProcessor logoutServiceProcessor) {
        CloseReqHandler handler = CommandManager.getCommand(Command.COMMAND_CLOSE_REQ, CloseReqHandler.class);
        handler.setSingleProcessor(logoutServiceProcessor);
        log.info("============================加载【退出登录业务处理服务器】===========================");
        return handler;
    }


    /**
     * 自定义消息处理器
     * @return
     */
    @Bean
    public ChatMessageServiceProcessor chatMessageServiceProcessor(){
        return new ChatMessageServiceProcessor();
    }

    /**
     * 默认消息处理器
     * @return
     */
    @Bean
    public DefaultAsyncChatMessageProcessor defaultChatProcessor() {
        return new DefaultAsyncChatMessageProcessor();
    }

    /**
     * 【自定义业务】查询该用户下的所有群组
     */
    @Bean
    @Autowired
    public GetGroupListByUserIdHandle groupListHandler(GetGroupListServiceProcessor groupProcessor) {
        GetGroupListByUserIdHandle handler = CommandManager.getCommand(Command.COMMAND_GET_GROUP_List_BY_USER, GetGroupListByUserIdHandle.class);
        handler.setSingleProcessor(groupProcessor);
        log.info("============================加载【查询用户下群组业务处理服务】===========================");
        return handler;
    }

    /***
     * 【自定义业务】查询该群组下所有成员
     */
    @Bean
    @Autowired
    public UserListByGroupReqHandler groupMemberHandler(GroupServiceProcessor memberProcessor) {
        UserListByGroupReqHandler handler = CommandManager.getCommand(Command.COMMAND_GET_USER_LIST_BY_GROUP, UserListByGroupReqHandler.class);
        handler.setSingleProcessor(memberProcessor);
        log.info("============================加载【查询群组成员业务处理服务】===========================");
        return handler;
    }

    /**
     * @author: lml
     * 申请加入群组 群组处理器
     */
    @Bean
    @Autowired
    public JoinGroupReqHandler groupReqHandler(GroupServiceProcessor groupServiceProcessor) {
        JoinGroupReqHandler handler = CommandManager.getCommand(Command.COMMAND_JOIN_GROUP_REQ,JoinGroupReqHandler.class);
        handler.setSingleProcessor(groupServiceProcessor);
        log.info("============================加载【申请加入群组业务处理服务】===========================");
        return handler;
    }

    /**
     * @author: lml
     * 创建群组
     */
    @Bean
    @Autowired
    public CreateGroupHandler createGroupReqHandler(GroupServiceProcessor groupServiceProcessor) {
        CreateGroupHandler handler = CommandManager.getCommand(Command.COMMAND_CREATE_GROUP_REQ, CreateGroupHandler.class);
        handler.setSingleProcessor(groupServiceProcessor);
        log.info("============================加载【申请创建群组业务处理服务】===========================");
        return handler;
    }
}